<?php


namespace app\common\controller;


use app\common\model\CardusCodeMaster;
use app\common\model\CardUserBonus;
use app\common\model\CardUserFee;
use app\common\model\CardUserGet;
use app\common\model\CardUserRecLog;
use think\Controller;
use Wechat\Wxobjopen;

class ChildCardModify extends Controller
{

    public function createmodify($type, $bus_orderid)
    {


        if ($type == "balance_rec") {
            $CardUserFee = new CardUserFee();
            $CardUserBonus = new CardUserBonus();
            $CardUserRecLog = new CardUserRecLog();
            $map['bus_orderid'] = $bus_orderid;
            $cardreclog = $CardUserRecLog->where($map)->field('give_fee_id,rec_fee_id,bonus_id,action_user,create_time,mid')->find();

            $givefee = $CardUserFee->where(['id' => $cardreclog['give_fee_id']])->value('num');

            $recinfo = $CardUserFee->where(['id' => $cardreclog['rec_fee_id']])->field('num,card_id,code,openid')->find();

            $bonus = $CardUserBonus->where(['id' => $cardreclog['bonus_id']])->value('num');

            $param['card_id'] = $recinfo['card_id'];
            $param['code'] = $recinfo['code'];
            $param['type'] = "inc";

            if ($bonus) {
                $param['cate'] = "bonus";
                $param['num'] = $bonus;
//                var_dump($param);
                $this->childmodify($param);
            }
            if ($givefee + $recinfo['num'] > 0) {
                $param['cate'] = "balance";
                $param['num'] = $givefee + $recinfo['num'];
              
//                var_dump($param);
                $this->childmodify($param);
            }


        } elseif ($type == "balance_con") {
            $CardUserFee = new CardUserFee();
            $recinfo = $CardUserFee->where(['bus_orderid' => $bus_orderid])->field('card_id,code,openid,num')->find();
            $param['card_id'] = $recinfo['card_id'];
            $param['code'] = $recinfo['code'];
            $param['type'] = "dec";
            $param['cate'] = "balance";
            $param['num'] = $recinfo['num'];
            $this->childmodify($param);
        } elseif ($type == "bonus_rec") {
            $CardUserBonus = new CardUserBonus();
            $map['bus_orderid'] = $bus_orderid;
            $recinfo = $CardUserBonus->where($map)->find();
            $param['card_id'] = $recinfo['card_id'];
            $param['code'] = $recinfo['code'];
            $param['type'] = "inc";
            $param['cate'] = "bonus";
            $param['num'] = $recinfo['num'];
            $this->childmodify($param);
        } elseif ($type == "bonus_con") {
            $CardUserBonus = new CardUserBonus();
            $map['bus_orderid'] = $bus_orderid;
            $recinfo = $CardUserBonus->where($map)->find();
            $param['card_id'] = $recinfo['card_id'];
            $param['code'] = $recinfo['code'];
            $param['type'] = "dec";
            $param['cate'] = "bonus";
            $param['num'] = $recinfo['num'];
            $this->childmodify($param);
        }


    }


    /*
    * 母卡积分余额变动
    * card_id:子卡的card_id
    * code:子卡的code
    * type: inc 增加 dec 减少
    * cate: balance 余额  bonus 积分
    * num:变动数量
    *
    * */

    public function childmodify($param)
    {
        //查询卡信息
        $CardUserGet = new CardUserGet();
        $map['card_id'] = $param['card_id'];
        $map['code'] = $param['code'];
        $carduserinfo = $CardUserGet->where($map)->field('id,master_id,is_shared,is_shared_bonus,is_child')->find();
        //判断是否子卡
        if ($carduserinfo['is_child'] == "Y" && $carduserinfo['master_id'] > 0) {
            //判断是否开启账户共享
//            if ($carduserinfo['is_shared'] == "Y") {
                //变更母卡积分余额
                $CardUsCoDeMaster = new CardusCodeMaster();
                $map3['id'] = $carduserinfo['master_id'];

                if ($param['type'] == "inc") {


                    $CardUsCoDeMaster->where($map3)->setInc($param['cate'], $param['num']);
                } elseif ($param['type'] == "dec") {

                    $CardUsCoDeMaster->where($map3)->setDec($param['cate'], $param['num']);

                }
                //查询出所有可用积分/余额的子卡
                if ($param['cate'] == 'balance') {
                    $map2['is_shared'] = "Y";
                }else{
                    $map2['is_shared_bonus'] = "Y";
                }
                //查询出所有子卡
                $map2['is_shared'] = "Y";
                $map2['card_id'] = $param['card_id'];
                $map2['master_id'] = $carduserinfo['master_id'];
                $map2['id'] = array('neq', $carduserinfo['id']);
                $carduserlist = $CardUserGet->where($map2)->field('card_id,code,id')->select();
                //变更所有子卡积分余额
                foreach ($carduserlist as $k => $v) {
                    $this->upddateinfo($param['cate'], $param['type'], $v['id'], $param['num']);
                    if ($param['cate'] == "bonus") {
                        $this->updatebonus($v['card_id'], $v['code'], $param['type'], $param['num']);
                    }
                }


//            }
        }
    }

    /*
     * 更新用户数据库记录 2018-09-11
     *
     * cate  balance 余额  bonus 积分
     * num 变动数量
     * */
    public function upddateinfo($cate, $type, $id, $num)
    {

        $CardUserGet = new CardUserGet();
        if ($type == "inc") {

            $CardUserGet->where('id', $id)->setInc($cate, $num);
        } elseif ($type == "dec") {
            $CardUserGet->where('id', $id)->setDec($cate, $num);
        }
//        //更新使用频次
//        $CardUserGet->where('code', $code)->setInc('frequency', 1);
//        $CardUserGet->allowField(true)->save(['last_pay_time'=>time()],['code'=>$code]);

    }

    /*
     * type 1 加 2 减
     * */
    private function updatebonus($card_id, $code, $type, $num)
    {
        //实例化微信
        $WxOption = new Wxoption();
        $options = $WxOption->options($card_id, 'card_id');
        $WxObj = new Wxobjopen($options);
        $nums = $type == "inc" ? $num : -$num;
        $data = [
            'code' => $code,
            'card_id' => $card_id,
            'record_bonus' => "",       //说明
//            'bonus' => $this->getuserbonus(),
            'add_bonus' => $nums,             //积分变动值  负数为减少
            'notify_optional' => [
                'is_notify_bonus' => false,
                'is_notify_balance' => false,
                'is_notify_custom_field1' => false,
            ],
        ];
        $res = $WxObj->updateUserInfo($data);
        if ($res['errcode'] == 0) {
            $Level = new Level();
            $Level->checklevel($card_id, $code, $res['result_bonus']);
        }

        return $res;
    }

    /*
    *
    *余额变动模板消息通知
    *
    */
    private function sendbalancemsg($param)
    {
        $arr['card_id'] = $param['card_id'];
        $arr['type'] = $param['cate'];
        $arr['openid'] = $param['openid'];
        $arr['add_balance'] = $param['cz_balance'] + $param['zs_balance'];
        $arr['balance'] = $this->getuserfee($param['id']);

        $TemplateMsg = new TemplateMsg();
        $TemplateMsg->send_tpl('pay_balance', $arr);

    }

    /*
    *
    *获取用户余额
    *
    */
    private function getuserfee($id)
    {
        $CardUserGet = new CardUserGet();
        $user = $CardUserGet->where('id', $id)->value('balance');
        return $user;
    }

}